//
//  STGetBackPwdViewController.m
//  赛拓
//
//  Created by 王磊 on 7/31/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STGetBackPwdViewController.h"
#import "STCustomTF.h"
@interface STGetBackPwdViewController ()<STBaseViewDelegate>

@property (nonatomic ,strong) STCustomTF *phone;

@property (nonatomic ,strong) STCustomTF *verify;

@property (nonatomic ,strong) STCustomTF *pwd;

@property (nonatomic ,strong) NSTimer *timer;

@property (nonatomic ,assign) NSInteger count;
@end

@implementation STGetBackPwdViewController
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    int a = 1;
    
    if (self.timer) {
        
        [self.timer invalidate];
        
        self.timer = nil;
        
        [self.verify updateVerifyState:@"点击获取" isEnabled:true];
    }
    
    [self.phone clear];
    
    [self.verify clear];
    
    [self.pwd clear];
}
- (void)initSubviews {
    [super initSubviews];
    
    self.title = @"找回密码";
    
    CGFloat height = 85 * ST_RATE;
    
    CGFloat left_margin = 40 * ST_RATE;
    
    CGFloat login_topMargin = 60 * ST_RATE;
    
    self.view.backgroundColor = ST_WHITE_COLOR;
    
    STCustomTF *phone = [STCustomTF instance];
    
    phone.mDelegate = self;
    
    self.phone = phone;
    
    phone.placeHolder = @"请输入手机号";
    
    phone.tid = @"phone";
    
    phone.maxLength = 11;
    
    phone.iconImageName = @"手机";
    
    phone.returnKeyType = UIReturnKeyNext;
    
    [self.view addSubview:phone];
    
    STCustomTF *verify = [STCustomTF instance];
    
    self.verify = verify;
    
    verify.isVerifyShow = false;
    
    verify.maxLength = 4;
    
    verify.mDelegate = self;
    
    verify.tid = @"verify";
    
    verify.iconImageName = @"验证码";
    
    verify.returnKeyType = UIReturnKeyNext;
    
    verify.placeHolder = @"请输入验证码";
    
    [self.view addSubview:verify];
    
    STCustomTF *pwd = [STCustomTF instance];
    
    self.pwd = pwd;
    
    pwd.mDelegate = self;
    
    pwd.tid = @"pwd";
    
    pwd.maxLength = 18;
    
    pwd.iconImageName = @"密码";
    
    pwd.secureTextEntry = true;
    
    pwd.returnKeyType = UIReturnKeyDone;
    
    pwd.placeHolder = @"请输入密码";
    
    [self.view addSubview:pwd];
    [phone makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(90 * ST_RATE);
        make.height.equalTo(height);
        make.left.equalTo(left_margin);
        make.right.equalTo(-left_margin);
    }];
    [verify makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phone.mas_bottom).offset(20 * ST_RATE);
        make.height.equalTo(height);
        make.left.equalTo(left_margin);
        make.right.equalTo(-left_margin);
    }];
    
    //    @WeakObj(self)
    [pwd makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(verify.mas_bottom).offset(20 * ST_RATE);
        make.height.equalTo(height);
        make.left.equalTo(left_margin);
        make.right.equalTo(-left_margin);
    }];
    
    
    UIButton *complete = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.view addSubview:complete];
    
    [complete setTitle:@"完成" forState:UIControlStateNormal];
    
    [complete setTitle:@"完成" forState:UIControlStateHighlighted];
    
    complete.backgroundColor = ST_167EC0_COLOR;
    
    complete.layer.cornerRadius = height / 2;
    
    [complete setTitleColor:ST_WHITE_COLOR forState:UIControlStateNormal];
    
    [complete setTitleColor:ST_WHITE_COLOR forState:UIControlStateHighlighted];
    
    [complete makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pwd.mas_left);
        make.right.equalTo(pwd.mas_right);
        make.top.equalTo(pwd.mas_bottom).offset(login_topMargin);
        make.height.equalTo(height);
    }];
    
    [complete addTarget:self action:@selector(completeTriggered) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)completeTriggered {
    
    [self onItemClick:STItemClickTypePwdBack withData:nil forIndexPath:nil];
}
- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    //    @WeakObj(self)
    switch (itemType) {
        case STItemClickTypeVerify:
        {
            self.count = 60;
            @WeakObj(self)
            if (self.phone.text.length) {
                
                [[STHttpManager getInstance] requestWithUrlString:ST_HTTP_GETVCODE andParams:@{@"phoneNum":self.phone.text} andStartBlock:^BOOL{
                    return true;
                } andSuccessBlock:^(NSString *rescode, NSString *msg, NSString *token, NSString *sysTime, id data, NSString *page) {
                    if ([rescode  isEqualToString:@"100"]) {
                        
                        selfWeak.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timeChanged:)userInfo:nil repeats:true];
                        
                        [[NSRunLoop currentRunLoop]addTimer:selfWeak.timer forMode:NSDefaultRunLoopMode];
                        
                    } else {
                        
                        [SVProgressHUD showInfoWithStatus:msg];
                    }
                    
                } andFailureBlock:^BOOL{
                    
                    return true;
                }];
            } else {
                
                [SVProgressHUD showInfoWithStatus:@"手机号不能为空"];
            }
        }
            break;
            
        case STItemClickTypeReturnKey:
            if ([data isEqualToString:@"phone"]) {
                
                [self.phone resignFirstResponder];
                
                [self.verify becomeFirstResponder];
                
            }else if ([data isEqualToString:@"verify"]) {
                
                [self.verify resignFirstResponder];
                
                [self.pwd becomeFirstResponder];
            } else {
                
                [self.pwd resignFirstResponder];
                
                [self completeTriggered];
            }
            break;
            
        case STItemClickTypePwdBack: {
            
            if (self.phone.text.length && self.verify.text.length && self.pwd.text.length) {
                @WeakObj(self)
                [[STHttpManager getInstance] requestWithUrlString:ST_HTTP_GETVCODE andParams:@{@"phoneNum":self.phone.text,@"vcode":@"",@"password":@""} andStartBlock:^BOOL{
                    return true;
                } andSuccessBlock:^(NSString *rescode, NSString *msg, NSString *token, NSString *sysTime, id data, NSString *page) {
                    [SVProgressHUD showInfoWithStatus:msg];
                    
                    [selfWeak.navigationController popViewControllerAnimated:true];
                    
                } andFailureBlock:^BOOL{
                    
                    return true;
                }];
                
            } else {
                if (!self.verify.text.length) {
                    [SVProgressHUD showInfoWithStatus:@"验证码不能为空"];
                }
                if (!self.phone.text.length || !self.pwd.text.length) {
                    [SVProgressHUD showInfoWithStatus:@"手机号或密码不能为空"];
                }
            }
            
        }
            break;
        default:
            break;
    }
}
- (void)timeChanged:(NSTimer *)timer {
    self.count--;
    
    BOOL enbled = false;
    NSString *text = @"";
    if (self.count <= 0) {
        [self.timer invalidate];
        self.timer = nil;
        enbled = true;
        text = @"点击获取";
        
    }else {
        
        text = [NSString stringWithFormat:@"(%lds)",self.count];
    }
    
    [self.verify updateVerifyState:text isEnabled:enbled];
    
}

@end
